/*
 * Background.cpp
 *
 *  Created on: 2011-5-5
 *      Author: Administrator
 */

#include "Background.h"
#include <FSystem.h>
#include <FMedia.h>

using namespace Osp::Graphics;
using namespace Osp::Media;
using namespace Osp::System;
using namespace Osp::Base;
using namespace Osp::Base::Utility;

namespace Galaxy {
Background::Background() {
	Image decoder;
	decoder.Construct();
	__pBackgroundImage = decoder.DecodeN(L"/Home/nebula2.png",Osp::Graphics::BITMAP_PIXEL_FORMAT_ARGB8888);
	if(__pBackgroundImage){
		AppLog("__pBackgroundBitmap is null");
	}

	__nbStars = 0;
	TimeSpan timeSpan(1);
	SystemTime::GetUptime(timeSpan);
	__lastTime = timeSpan.GetMilliseconds();
}

Background::~Background() {

}

void Background::Init(){
	AppLog("Background::Init()---Begin");
	__nbStars = 20;
	__pStars = new int[__nbStars*3];
	for(int i = 0; i < __nbStars; i++){
		__pStars[i*3 + 0] = 1;
		__pStars[i*3 + 1] = Math::Rand()%480;
		__pStars[i*3 + 2] = Math::Rand()%800;
	}

	AppLog("Background::Init()---End");
}

void Background::DoPhisic(long now){
	//TODO
	int duration = int(now - this->__lastTime);
	if(duration < 3000){
		return;
	}
	__lastTime = now;
	int nbStars = this->__nbStars;
	int* pStars = this->__pStars;
	for(int i = 0; i < nbStars; i++){
		if(Math::Rand()%100 < 10){
			int index = i*3;
			pStars[index] = Math::Rand()%5;
		}
	}

}

void Background::DoDraw(Osp::Graphics::Canvas* pCanvas){
	AppLog("Background::DoDraw---Begin");

	if( pCanvas == NULL ){
		return;
	}
	pCanvas->DrawBitmap(Point(0,0), *__pBackgroundImage);

	int nbStars = this->__nbStars;
	int* pStars = this->__pStars;
	for( int i = 0; i < nbStars; i++ ){
		int index = i*3;
		int state = pStars[index];
		int x = pStars[index + 1];
		int y = pStars[index + 2];

		AppLog("Background::DoDraw, state = %d", state);
		AppLog("Background::DoDraw, x = %d", x);
		AppLog("Background::DoDraw, y = %d", y);

		Color color;
		switch(state){
		case 0:
			color = Color::COLOR_WHITE;
			break;
		case 1:
			color = Color::COLOR_RED;
			break;
		case 2:
			color = Color::COLOR_YELLOW;
			break;
		case 3:
			color = Color::COLOR_GREY;
			break;
		case 4:
			color = Color::COLOR_GREY;
			break;
		default:
			color = Color::COLOR_WHITE;
			break;
		}
		pCanvas->FillEllipse(color,Rectangle(x,y,2,2));
	}

	AppLog("Background::DoDraw---End");
}

}

